package cn.tannn.cat.file.sdk.core.qiniu;

import cn.tannn.cat.file.sdk.bean.FileStorage;
import cn.tannn.cat.file.sdk.config.QiNiuConfig;
import cn.tannn.cat.file.sdk.core.OssCache;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

;

/**
 * 七牛
 *
 * @author <a href="https://tannn.cn/">tnnn</a>
 * @version V1.0
 * @date 2024/4/5 下午10:15
 */
@Slf4j
public class QiNiuUtils {

    /**
     * 创建 QiNiu client cache
     *
     * @param storage QiNiu{@link FileStorage}
     */
    public static void createQiNiuClient(FileStorage storage) {
        try {
            QiNiuConfig qiNiuConfig = storage.getConfig().toJavaObject(QiNiuConfig.class);
            if (Objects.nonNull(qiNiuConfig)) {
                // ID 是唯一的
                QiNiuClient qiNiuClient = new QiNiuClient();
                Auth auth = auth(qiNiuConfig);
                Configuration configuration = qiniuConfig(qiNiuConfig);
                UploadManager uploadManager = uploadManager(configuration);
                BucketManager bucketManager = bucketManager(auth, configuration);
                qiNiuClient.setUploadManager(uploadManager);
                qiNiuClient.setBucketManager(bucketManager);
                qiNiuClient.setAuth(auth);
                OssCache.qiNiuClientCache.put(storage.getId(), qiNiuClient);
            }
        } catch (Exception e) {
            log.error("七牛《{}》配置异常，请检查", storage.getName(), e);
        }
    }

    /**
     * 获取上传凭证
     *
     * @param bucket 桶名
     * @param auth   认证信息
     * @return String
     */
    public static String getUploadToken(String bucket, Auth auth) {
        //上传策略
        StringMap policy = new StringMap();
        return auth.uploadToken(bucket, null, 3600, policy);
    }


    /**
     * 配置自己空间所在的区域
     *
     * @param qiNiuConfig QiNiuConfig
     * @return Configuration
     */
    private static Configuration qiniuConfig(QiNiuConfig qiNiuConfig) {

        Region region;
        if(qiNiuConfig.getRegionId() == null){
            region = Region.autoRegion();
        }else {
            region =  Region.createWithRegionId(qiNiuConfig.getRegionId());
        }
        return Configuration.create(region);
    }

    /**
     * 构建一个七牛上传工具实例
     *
     * @param fileConfig QiNiuConfig
     * @return UploadManager
     */
    private static UploadManager uploadManager(QiNiuConfig fileConfig) {
        return new UploadManager(qiniuConfig(fileConfig));
    }

    /**
     * 构建一个七牛上传工具实例
     *
     * @param configuration Configuration
     * @return UploadManager
     */
    private static UploadManager uploadManager(Configuration configuration) {
        return new UploadManager(configuration);
    }


    /**
     * 认证信息实例
     *
     * @param fileConfig QiNiuConfig
     * @return Auth
     */
    private static Auth auth(QiNiuConfig fileConfig) {
        return Auth.create(fileConfig.getAccessKey(), fileConfig.getSecretKey());
    }

    /**
     * 构建七牛空间管理实例
     *
     * @param fileConfig QiNiuConfig
     * @return BucketManager
     */
    private static BucketManager bucketManager(QiNiuConfig fileConfig) {
        return new BucketManager(auth(fileConfig), qiniuConfig(fileConfig));
    }


    /**
     * 构建七牛空间管理实例
     *
     * @param auth        Auth
     * @param qiniuConfig Configuration
     * @return BucketManager
     */
    private static BucketManager bucketManager(Auth auth, Configuration qiniuConfig) {
        return new BucketManager(auth, qiniuConfig);
    }



    /**
     * 删除 QiNiu client cache
     *
     * @param fileStorageId 存储器ID
     */
    public static void deleteQiNiuClient(Long fileStorageId) {
        try {
            OssCache.qiNiuClientCache.remove(fileStorageId);
        } catch (Exception e) {
            log.error("删除七牛 client cache 失败 fileStorageId {}", fileStorageId, e);
        }
    }
}
